package com.ptr.ptrsystem.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ptr.controladores.conexionsqlserver;
import com.ptr.ptrsystem.bean.Ubigeo;
import java.sql.*;
import com.ptr.ptrsystem.dao.Lista;
import java.util.LinkedList;

/**
 *
 * @author WALDIR
 */
@WebServlet(name = "BusquedaUbigeoServlet", urlPatterns = {"/BusquedaUbigeo"})
public class BusquedaUbigeoServlet extends HttpServlet {

    conexionsqlserver con = new conexionsqlserver();
    Connection cn = null;
    Statement st = null;
    ResultSet rs = null;
    String QUERY = "";
    String dep;
    String pro;
    String dis;
    String otr;
    String depM;
    String proM;
    String disM;
    String otrM;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String boton = request.getParameter("boton");

        if (boton.equals("UbigeoM")) {
            depM = request.getParameter("TdepartamentoM").toUpperCase();
            proM = request.getParameter("TprovinciaM").toUpperCase();
            disM = request.getParameter("TdistritoM").toUpperCase();
            otrM = request.getParameter("TotroM").toUpperCase();

            BusquedaUbigeoM(request, response);
        }

        if (boton.equals("Ubigeo")) {
            dep = request.getParameter("Tdepartamento").toUpperCase();
            pro = request.getParameter("Tprovincia").toUpperCase();
            dis = request.getParameter("Tdistrito").toUpperCase();
            otr = request.getParameter("Totro").toUpperCase();
            BusquedaUbigeo(request, response);
        }
    }

    protected void BusquedaUbigeo(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            QUERY = "select * from TB_UBIGEO where nom_ubigeo_dep like('%" + dep + "%')"
                    + " and nom_ubigeo_prov like('%" + pro + "%') and nom_ubigeo_dist like('%" + dis + "%') "
                    + "and nom_ubigeo_otro like('%" + otr + "%')";
            con.conectar();
            cn = con.getconection();
            st = cn.createStatement();
            rs = st.executeQuery(QUERY);
            while (rs.next()) {
                out.println("<tr id=" + rs.getString(1) + ">"
                        + "<td align='center'>" + rs.getString(2) + "</td>"
                        + "<td align='center'>" + rs.getString(3) + "</td>"
                        + "<td align='center'>" + rs.getString(4) + "</td>"
                        + "<td align='center'>" + rs.getString(5) + "</td>"
                        + "<td align='center'>" + rs.getString(6) + "</td>"
                        + "<td align='center'>" + rs.getString(7) + "</td>"
                        + "<td align='center'>" + rs.getString(8) + "</td>"
                        + "<td align='center'>" + rs.getString(9) + "</td>"
                        + "<td align='center'><input type='image' name='seleccionar' src='/RegCivil/imagenes/apply.png' alt='borrar' value=" + rs.getString(1) + " onclick='selectUbigeo(this)'/></td>"
                        + "</tr>");
            }
            con.cerrarBD();
            cn.close();
            st.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void BusquedaUbigeoM(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            QUERY = "select * from TB_UBIGEO where nom_ubigeo_dep like('%" + depM + "%')"
                    + " and nom_ubigeo_prov like('%" + proM + "%') and nom_ubigeo_dist like('%" + disM + "%') "
                    + "and nom_ubigeo_otro like('%" + otrM + "%')";
            con.conectar();
            cn = con.getconection();
            st = cn.createStatement();
            rs = st.executeQuery(QUERY);
            while (rs.next()) {
                out.println("<tr id=" + rs.getString(1) + ">"
                        + "<td align='center'>" + rs.getString(2) + "</td>"
                        + "<td align='center'>" + rs.getString(3) + "</td>"
                        + "<td align='center'>" + rs.getString(4) + "</td>"
                        + "<td align='center'>" + rs.getString(5) + "</td>"
                        + "<td align='center'>" + rs.getString(6) + "</td>"
                        + "<td align='center'>" + rs.getString(7) + "</td>"
                        + "<td align='center'>" + rs.getString(8) + "</td>"
                        + "<td align='center'>" + rs.getString(9) + "</td>"
                        + "<td align='center'><input type='image' name='seleccionar' src='/RegCivil/imagenes/apply.png' alt='borrar' value=" + rs.getString(1) + " onclick='selectUbigeoM(this)'/></td>"
                        + "</tr>");
            }
            con.cerrarBD();
            cn.close();
            st.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
